package com.Bigbluebook.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.Bigbluebook.Activity.PostDetailsActivity;
import com.Bigbluebook.R;
import com.Bigbluebook.adapter.PostsInfoListAdapter;
import com.Bigbluebook.api.ApiConstants;
import com.Bigbluebook.entity.PostsInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class TabNewsFragment extends Fragment {

    private static final String ARG_PARAM1 = "param";
    private String tag;


    private RecyclerView recyclerView;
    private PostsInfoListAdapter  postsInfoListAdapter;
    private View rootView;
    private EditText et_content;
    private TextView btn_search;

    private List<PostsInfo> searchList=new ArrayList<>();
    private List<PostsInfo> postsInfoList =new ArrayList<>();


    public TabNewsFragment() {
        // Required empty public constructor
    }

    public static TabNewsFragment newInstance(String param) {
        TabNewsFragment fragment = new TabNewsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            tag = getArguments().getString(ARG_PARAM1);
            Log.d("---------------------", "onCreate: "+tag);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView =inflater.inflate(R.layout.fragment_tab_news, container, false);


        //初始化控件

        recyclerView =rootView.findViewById(R.id.recyclerView);
        btn_search=rootView.findViewById(R.id.btn_search);
        et_content =rootView.findViewById(R.id.et_content);

        //初始化适配器
        postsInfoListAdapter =new PostsInfoListAdapter();

        //设置适配器
        recyclerView.setAdapter(postsInfoListAdapter);


        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //根据标签，获取文章列表
        getPostsByTagListData();

        // 设置RecyclerView item点击事件
        postsInfoListAdapter.setOnItemClickListener(new PostsInfoListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(PostsInfo post) {
                // 启动PostActivity并传递postId
                Intent intent = new Intent(getActivity(), PostDetailsActivity.class);
                intent.putExtra("postId", post.getId());
                startActivity(intent);
            }
        });

        //搜索点击事件
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Editable text = et_content.getText();
                if (TextUtils.isEmpty(text)){
                    Toast.makeText(getActivity(), "搜索内容不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                searchList.clear();
                for (int i = 0; i <postsInfoList.size() ; i++) {
                     if (postsInfoList.get(i).getContent().contains(text)){
                         searchList.add(postsInfoList.get(i));
                     }
                }
                //更新数据
                postsInfoListAdapter.setPostsInfoList(searchList);

            }
        });

        //监听输框文本变化
        et_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {

                String string = s.toString();
                if (TextUtils.isEmpty(string)){
                    //重新获取数据
                    getPostsByTagListData();
                }

            }
        });


    }

    private void  getPostsByTagListData(){
        OkGo.<String>get(ApiConstants.BASE_URL+"posts/tag/"+tag)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        Type listType = new TypeToken<List<PostsInfo>>() {}.getType();
                        postsInfoList = gson.fromJson(response.body(), listType);
                        //设置数据
                        postsInfoListAdapter.setPostsInfoList(postsInfoList);

                    }
                });
    }
}